﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Imaging;
using System.Windows.Media;

namespace EnsureWeb.Controls
{
    public class Expander : StackPanel
    {
        public Expander()
            : base()
        {
            titlePanel = new StackPanel();
            titlePanel.Orientation = Orientation.Horizontal;
            titlePanel.Margin = new Thickness(-30, 0, 0, 0);

            titleIcon = new ImageButton();
            titleIcon.Margin = new Thickness(2);
            titleIcon.NormalImage = new BitmapImage(new Uri("/EnsureWeb.Controls;component/Images/arrow_down_nomal.png", UriKind.RelativeOrAbsolute));
            titleIcon.HoverImage = new BitmapImage(new Uri("/EnsureWeb.Controls;component/Images/arrow_down_hover.png", UriKind.RelativeOrAbsolute));
            titleIcon.ClickImage = new BitmapImage(new Uri("/EnsureWeb.Controls;component/Images/arrow_down_click.png", UriKind.RelativeOrAbsolute));
            titleIcon.Click += new RoutedEventHandler(titleIcon_Click);


            titleIconRotate = new RotateTransform();
            titleIcon.RenderTransformOrigin = new Point(0.5, 0.5);
            titleIcon.RenderTransform = titleIconRotate;

            titleText = new TextBlock();
            titleText.Margin = new Thickness(2);
            titleText.FontSize = 13;
            titleText.Foreground = new SolidColorBrush(Color.FromArgb(0xFF, 0x0D, 0x36, 0x55));

            titlePanel.Children.Add(titleIcon);
            titlePanel.Children.Add(titleText);

            this.Children.Add(titlePanel);

            this.Orientation = Orientation.Vertical;
            this.RenderTransform = new TranslateTransform() { X = 30 };
        }



        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { 
                SetValue(TitleProperty, value);
                this.titleText.Text = value;
            }
        }

        // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(Expander), new PropertyMetadata(string.Empty));


        private bool IsExpand
        {
            set
            {
                if (value)
                    Expand();
                else
                    Collapsible();
            }
        }

        void titleIcon_Click(object sender, RoutedEventArgs e)
        {
            if (isCollapsible)
            {
                Expand();
            }
            else
            {
                Collapsible();
            }
        }

        public void Expand()
        {
            foreach (UIElement element in this.Children)
                element.Visibility = Visibility.Visible;

            isCollapsible = false;

            titleIconRotate.Angle = 0;

            titleText.TextDecorations = null;
        }

        public void Collapsible()
        {
            foreach (UIElement element in this.Children)
                element.Visibility = Visibility.Collapsed;

            titlePanel.Visibility = Visibility.Visible;
            isCollapsible = true;

            titleIconRotate.Angle = -90;

            titleText.TextDecorations = TextDecorations.Underline;
        }

        private StackPanel titlePanel;
        private ImageButton titleIcon;
        private RotateTransform titleIconRotate;
        private TextBlock titleText;
        private bool isCollapsible;

    }
}
